﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmPhieuNhapChiTiet : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        public static int hientrang = 0;
        private PhieuNhapChiTietBOCollection PhieuNhapChiTietBOCollection = PhieuNhapChiTietBOCollection.NewPhieuNhapChiTietBOCollection();
        private PhieuNhapBO PhieuNhapBO = PhieuNhapBO.NewRootPhieuNhapBO();

        private PhieuNhapChiTietBOCollection PhieuNhapChiTietBOCollection1 = PhieuNhapChiTietBOCollection.NewPhieuNhapChiTietBOCollection();
        private PhieuNhapBO PhieuNhapBO1 = PhieuNhapBO.NewRootPhieuNhapBO();

        #endregion variant

        //=====================================
        #region form
        public FrmPhieuNhapChiTiet(PhieuNhapBO hoaDonBanBO, PhieuNhapChiTietBOCollection hoaDonBanChiTietBOCollection)
        {
            InitializeComponent();
            this.KeyPreview = true;


            this.PhieuNhapBO = this.PhieuNhapBO1 = hoaDonBanBO;
            this.PhieuNhapChiTietBOCollection = this.PhieuNhapChiTietBOCollection1 = hoaDonBanChiTietBOCollection;

            this.Load += new EventHandler(FrmPhieuNhapChiTiet_Load);
            this.Shown += new EventHandler(FrmPhieuNhapChiTiet_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmPhieuNhapChiTiet_FormClosing);
            this.KeyDown += new KeyEventHandler(this.FrmHoadonnhap_KeyDown);

            this.btnInvaLuu.Click += new EventHandler(btnPrinSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

        }

        private void FrmPhieuNhapChiTiet_Shown(object sender, EventArgs e)
        {
            this.lookUpEditKhachHang.Focus();
        }

        private void FrmPhieuNhapChiTiet_Load(object sender, EventArgs e)
        {
            //SetFormStatus(this.formStatus);


            Utility.InitControl(this.reLookUpMatHang);
            Utility.InitControl(this.reLookUpDonViTinh);
            Utility.InitControl(this.reLookUpDMKhoID);

            Utility.InitControl(this.lookUpEditKhachHang);

            RefreshData();

            this.spinEditPhucVu.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditGiamGia.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditChietKhau.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditVAT.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);
            this.spinEditTienPhatSinh.ValueChanged += new System.EventHandler(this.spinEditTienPhatSinh_ValueChanged);

        }

        private void FrmPhieuNhapChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }

        private void FrmHoadonnhap_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            if (this.btnInvaLuu.Visible && ((e.KeyCode == Keys.P && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F7)))
                        btnPrinSave_Click(null,null);

        }
        #endregion form

        //=====================================
        #region control


        private void spinEditTienPhatSinh_ValueChanged(object sender, EventArgs e)
        {
            TinhTien();
        }
        #endregion control

        //=====================================
        #region button


        private void btnPrinSave_Click(object sender, EventArgs e)
        {
            if (StaticUtil.GetRealInt(PhieuNhapBO.PhieuNhapID.Value, -1) >= 0)
            {
                RPhieuNhapBO rPhieuNhapBO = RPhieuNhapBO.NewRootRPhieuNhapBO();
                rPhieuNhapBO.CopyFromRPhieuNhapBO(PhieuNhapBO);
                Report.rpPhieuNhapKho rPhieuNhapKho = new TanHungLong.Report.rpPhieuNhapKho();
                rPhieuNhapKho.SetReportData(rPhieuNhapBO, PhieuNhapChiTietBOCollection);
                rPhieuNhapKho.ShowPreviewDialog();
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion button

        //=====================================
        #region grid
        #endregion grid

        //=====================================
        #region function

        private void RefreshData()
        {
            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinh, donViTinhBOList, donViTinhBOList.Count);

            DanhMucBOList dmKhoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            Utility.SetData(this.reLookUpDMKhoID, dmKhoBOList, dmKhoBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reLookUpMatHang, matHangBOList, matHangBOList.Count);

            KhachHangBOList khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.lookUpEditKhachHang, khachHangBOList, khachHangBOList.Count);

            #endregion BOList

            if (StaticUtil.GetInt(this.PhieuNhapBO.PhieuNhapID, -1) >= 0)
            {
                lookUpEditKhachHang.EditValue = StaticUtil.GetInt(this.PhieuNhapBO.NhaCungCapID, -1);
                txtNguoiMuaHang.Text = this.PhieuNhapBO.NguoiGiaoHang;
                txtMaSo.Text = this.PhieuNhapBO.MaSo;
                dtNgayLapHoaDon.DateTime = this.PhieuNhapBO.NgayLapHoaDon.Value;
                txtNoidung.Text = this.PhieuNhapBO.NoiDung;
                txtGhichu.Text = this.PhieuNhapBO.GhiChu;

                sapxep();
                TinhTien();
            }
        }

        private void AddNew()
        {
            this.PhieuNhapBO = Model.PhieuNhapBO.NewRootPhieuNhapBO();
            this.PhieuNhapChiTietBOCollection = Model.PhieuNhapChiTietBOCollection.NewPhieuNhapChiTietBOCollection();
        }

        private void sapxep()
        {

            int length = PhieuNhapChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                PhieuNhapChiTietBOCollection[i].STT0 = (i + 1);
            }

            bdSourcePhieuNhapChiTiet.DataSource = PhieuNhapChiTietBOCollection;
        }

        private void TinhTien()
        {
            double tongso = 0;
            double thanhtien = 0;
            double tongsotralai = 0;
            double thanhtientralai = 0;
            double tongtien = 0;

            int length = PhieuNhapChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                tongso = tongso + PhieuNhapChiTietBOCollection[i].SoLuong.Value;
                thanhtien = thanhtien + PhieuNhapChiTietBOCollection[i].ThanhTien.Value;
            }

            spinEditTongSo.Value = (decimal)tongso;
            spinEditThanhTien.Value = (decimal)thanhtien;

            double tienKhachTra = StaticUtil.GetDouble(PhieuNhapBO.TienTra, 0);
            spinEditTienKhachTra.Value = (decimal)tienKhachTra;

            double mucVAT = StaticUtil.GetDouble(spinEditVAT.Value, 0);
            double tienVAT = thanhtien * mucVAT;
            spinEditTienVAT.Value = (decimal)tienVAT;
            double mucCK = StaticUtil.GetDouble(spinEditChietKhau.Value, 0);
            double tienCK = thanhtien * mucCK;
            spinEditTienChietKhau.Value = (decimal)tienCK;
            double mucGG = StaticUtil.GetDouble(spinEditGiamGia.Value, 0);
            double tienGG = thanhtien * mucGG;
            spinEditTienGiamGia.Value = (decimal)tienGG;
            double mucPV = StaticUtil.GetDouble(spinEditPhucVu.Value, 0);
            double tienPV = thanhtien * mucPV;
            spinEditTienPhucVu.Value = (decimal)tienPV;
            double TienPhatSinh = StaticUtil.GetDouble(spinEditTienPhatSinh.Value, 0);

            tongtien = thanhtien + tienVAT + TienPhatSinh - tienCK - tienGG + tienPV;
            spinEditTongTien.Value = (decimal)tongtien;
            txtBangChu.Text = VNCurrency.ToString(tongtien);
        }

        private void DeleteMatHang(int mathangID, int donvitinhID)
        {

            if (MessageBox.Show("Bạn có muốn xóa Mặt Hàng và Đơn vị tính này không? ", "Cảnh báo!!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                PhieuNhapChiTietBOCollection.Remove(mathangID, donvitinhID);
                int _idPhieuNhapChiTiet = StaticUtil.GetInt(this.grdViewPhieuNhapChiTiet.GetFocusedRowCellValue("PhieuNhapChiTietID"), -1);

                if (_idPhieuNhapChiTiet >= 0)
                {
                    PhieuNhapChiTietBO.DeletePhieuNhapChiTietBO(_idPhieuNhapChiTiet);
                }
                sapxep();
                TinhTien();
            }
        }

        private int kiemtraLuu()
        {
            int tempkiemtra = 1;
            string strError = "";

            //if (spinEditSoLuongTraLai.Value <= 0)
            //{
            //    tempkiemtra = 0;
            //    strError = "Chưa chọn Khách hàng";
            //}

            if (txtMaSo.Text.Trim() == "")
            {
                tempkiemtra = 0;
                strError = strError + " ;Chưa nhập Mã số";
            }

            if (tempkiemtra == 0)
                MessageBox.Show(strError, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return tempkiemtra;
        }

        #endregion function

    }
}